﻿<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:Brjnk.CoordinateSystem2D">

    <Style TargetType="{x:Type local:Area2D}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:Area2D}">
                    <ControlTemplate.Resources>
                        <BooleanToVisibilityConverter x:Key="VisibilityConverter" />
                        <Style TargetType="local:Axis">
                            <Setter Property="MaxFontZoom" Value="2" />
                            <Setter Property="TextBlock.FontSize" Value="10" />
                            <Setter Property="MinLabelWidth" Value="20" />
                            <Setter Property="LabelsMargin" Value="1" />
                        </Style>
                    </ControlTemplate.Resources>
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition />
                                <RowDefinition Height="Auto" />
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition />
                            </Grid.ColumnDefinitions>

                            <local:Axis x:Name="PART_AxisY"
                                        Width="30"
                                        Definition="{TemplateBinding AxisY}"
                                        From="{Binding RelativeSource={RelativeSource Mode=TemplatedParent},
                                                       Path=View.FromY}"
                                        MinLabelWidth="48"
                                        Orientation="Vertical"
                                        To="{Binding RelativeSource={RelativeSource Mode=TemplatedParent},
                                                     Path=View.ToY}"
                                        Visibility="{Binding RelativeSource={RelativeSource Mode=TemplatedParent},
                                                             Path=ShowAxisY,
                                                             Converter={StaticResource VisibilityConverter}}" />

                            <local:Axis x:Name="PART_AxisX"
                                        Grid.Row="1"
                                        Grid.Column="1"
                                        Height="30"
                                        Definition="{TemplateBinding AxisX}"
                                        From="{Binding RelativeSource={RelativeSource Mode=TemplatedParent},
                                                       Path=View.FromX}"
                                        MinLabelWidth="48"
                                        Orientation="Horizontal"
                                        To="{Binding RelativeSource={RelativeSource Mode=TemplatedParent},
                                                     Path=View.ToX}"
                                        Visibility="{Binding RelativeSource={RelativeSource Mode=TemplatedParent},
                                                             Path=ShowAxisX,
                                                             Converter={StaticResource VisibilityConverter}}" />

                            <ItemsControl Grid.Row="0"
                                          Grid.Column="1"
                                          ItemsSource="{Binding RelativeSource={RelativeSource Mode=TemplatedParent},
                                                                Path=Layers}">

                                <ItemsControl.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <Grid />
                                    </ItemsPanelTemplate>
                                </ItemsControl.ItemsPanel>
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <Border ClipToBounds="True"
                                                Opacity="{Binding Opacity}"
                                                Visibility="{Binding Visible,
                                                                     Converter={StaticResource VisibilityConverter}}">
                                            <ContentPresenter Content="{Binding Content}" />
                                        </Border>
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                            </ItemsControl>

                            <Border Name="PART_MouseObserver"
                                    Grid.Row="0"
                                    Grid.Column="1"
                                    Background="Transparent"
                                    BorderThickness="0" />

                            <Popup x:Name="PART_ToolTip" Placement="Mouse">
                                <StackPanel Background="LightYellow">
                                    <StackPanel Name="PART_CoordinatesInfo">
                                        <StackPanel Orientation="Horizontal">
                                            <TextBlock Text="X: " />
                                            <TextBlock Name="PART_LabelX" Text="{Binding}" />
                                        </StackPanel>
                                        <StackPanel Orientation="Horizontal">
                                            <TextBlock Text="Y: " />
                                            <TextBlock Name="PART_LabelY" Text="{Binding}" />
                                        </StackPanel>
                                    </StackPanel>
                                    <ItemsControl ItemsSource="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=Layers}">
                                        <ItemsControl.ItemsPanel>
                                            <ItemsPanelTemplate>
                                                <StackPanel Orientation="Vertical" />
                                            </ItemsPanelTemplate>
                                        </ItemsControl.ItemsPanel>
                                        <ItemsControl.ItemTemplate>
                                            <DataTemplate>
                                                <ContentPresenter Content="{Binding InfoContent}" />
                                            </DataTemplate>
                                        </ItemsControl.ItemTemplate>
                                    </ItemsControl>
                                    <StackPanel Name="PART_InfoCustomContent" Background="LightYellow" />
                                </StackPanel>
                            </Popup>

                            <Popup x:Name="PART_ContextMenu"
                                   Placement="Mouse"
                                   StaysOpen="False">
                                <StackPanel>
                                    <ItemsControl Background="White" ItemsSource="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=Layers}">
                                        <ItemsControl.ItemsPanel>
                                            <ItemsPanelTemplate>
                                                <StackPanel Orientation="Vertical" />
                                            </ItemsPanelTemplate>
                                        </ItemsControl.ItemsPanel>
                                        <ItemsControl.ItemTemplate>
                                            <DataTemplate>
                                                <ContentPresenter Content="{Binding MenuContent}" />
                                            </DataTemplate>
                                        </ItemsControl.ItemTemplate>
                                    </ItemsControl>
                                    <StackPanel Name="PART_MenuCustomContent" Background="White" />
                                </StackPanel>
                            </Popup>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>